const friendService = require('../service/friend.service')
const errorTypes = require('../contants/error-types')

const verifyApply = async (ctx, next) => {
  // 判断是否已经发送过请求了或者已经是好友，如果别人拒绝了请求可以再次发送
  const userId = ctx.user.id;
  const {f_id} = ctx.request.body
  const isApplied = await friendService.getApplyById(userId, f_id)
  if(isApplied) {
    const error = new Error(errorTypes.ALREADY_APPLIED)
    return ctx.app.emit('error', error, ctx)
  }

  await next()
}

module.exports = {
  verifyApply
}